#include "header.h" 

#ifdef _WIN32

#include <windows.h>
 
namespace uhdpbs {
	namespace menu {
		void VP_SetConsoleCursor(bool bCursor) {
			CONSOLE_CURSOR_INFO curCursorInfo;
			curCursorInfo.bVisible = bCursor;
			curCursorInfo.dwSize = 1;
			SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curCursorInfo);
		}
 
		void VP_GoToXY(int x, int y) {
			COORD CursorPosition = {x, y};
			SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CursorPosition);
		}
 
		void VP_SetConsoleColor(int textColor, int bgColor) {
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor * 16)));
		}
 
		void VP_ClearScreen() {
			CONSOLE_SCREEN_BUFFER_INFO csbi;
			HANDLE hStdOut;
			DWORD count;
			DWORD cellCount;
			COORD homeCoords = { 0, 0 };
 
			hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
			if (hStdOut == INVALID_HANDLE_VALUE) return;
 
			// Get the number of cells in the current buffer
			if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
			cellCount = csbi.dwSize.X *csbi.dwSize.Y;
 
			// Fill the entire buffer with spaces
			if (!FillConsoleOutputCharacter(hStdOut,(TCHAR) ' ', cellCount, homeCoords, &count))
				return;
 
			// Fill the entire buffer with the current colors and attributes
			if (!FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, cellCount, homeCoords, &count))
				return;
 
			// Move the cursor home
			SetConsoleCursorPosition( hStdOut, homeCoords );
			/* Windows */
		}
 
		int VP_GetCh () {
			/* Windows */
			HANDLE hConsole;
			INPUT_RECORD inrec;
			DWORD count, mode;
 
			// Set the console mode to no-echo, raw input, and no window or mouse events.
			hConsole = GetStdHandle(STD_INPUT_HANDLE);
			if (hConsole == INVALID_HANDLE_VALUE || !GetConsoleMode(hConsole, &mode)|| !SetConsoleMode(hConsole, 0))
				return 0;
 
			FlushConsoleInputBuffer(hConsole);
 
			// Get a single key RELEASE
			do {
				ReadConsoleInput(hConsole, &inrec, 1, &count);
			} while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
 
			// Restore the original console mode
			SetConsoleMode(hConsole, mode);
 
			return inrec.Event.KeyEvent.wVirtualKeyCode;
			/* Windows */
		}
	}
}
#endif
 
